package cn.lizemin.fakedata.project.elementUi.controller;

import cn.lizemin.fakedata.common.RespBean;
import cn.lizemin.fakedata.project.elementUi.entity.ElementUser;
import cn.lizemin.fakedata.project.elementUi.mapper.ElementUserMapper;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 李泽民
 * @Date 2024/2/21 16:04
 * @Description
 */
@RestController
@RequestMapping("/mybatis")
@Slf4j
public class MybatisCacheController {

    @Autowired
    private ElementUserMapper elementUserMapper;

    @GetMapping("/getUser")
    @Transactional //如果不加事务注解是看不出效果的，因为在没有事务的情况下，spring每次查数据库的sql，即便是同一条sql，也都会开启一个新的sqlSession, 同时关闭旧的session
    public RespBean getUser() {
        ElementUser user = elementUserMapper.getUser();
        log.info("第一次从数据库中查询到的用户对象:{}", JSON.toJSON(user));

        user.setAddress("中国深圳");
        log.info("已修改address");

        ElementUser user2 = elementUserMapper.getUser();
        log.info("第2次从数据库中查询到的用户对象:{}", JSON.toJSON(user2));
        return RespBean.success();
    }

}
